<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="renderer" content="webkit"/>
  <meta name="force-rendering" content="webkit"/>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
  <title>新增广告位</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="/bower_components/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="/bower_components/Ionicons/css/ionicons.min.css">
  <link rel="stylesheet" href="/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css">
  <link rel="stylesheet" href="/dist/css/AdminLTE.min.css">
  <link rel="stylesheet" href="/dist/css/skins/_all-skins.min.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->

  <!-- Google Font -->
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

  <!--头部-->
  <header id="headerDiv" class="main-header">
    <!--#include  file="header.html"-->
  </header>

  <!--左侧边栏 -->
  <aside id="sidebarDiv" class="main-sidebar">
    <!--#include  file="sidebar.html"-->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        <small>新建广告位</small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-gamepad"></i> 我的广告位</a></li>
        <li><a href="javascript:goBack();">广告位列表</a></li>
        <li><a href="javascript:void (0);">新建广告位</a></li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="row">
        <div class="col-xs-12">
          <div class="box box-primary">
            <div class="box-header with-border">
              <h3 class="box-title">新增广告位</h3>
            </div>
            <!-- /.box-header -->
            <!-- form start -->
            <form id="dataForm" class="form-horizontal">
              <div class="box-body">
                <div class="form-group">
                  <label for="siteid" class="col-sm-2 control-label">网站(app)</label>
                  <div class="col-sm-5">
                    <select id="siteid" onchange="initPuttype()"  class="form-control" name="siteid">

                    </select>
                  </div>
                </div>

                <div class="form-group">
                  <label  class="col-sm-2 control-label">投放模式</label>

                  <div class="col-sm-4">
                    <label class="radio-inline">
                      <input id="jstype" type="radio" value="0" disabled>JS投放</label>
                    <label class="radio-inline">
                      <input id="sdktype" type="radio" value="1" disabled>SDK投放</label>
                    <label class="radio-inline">
                      <input id="minitype" type="radio" value="2" disabled>小程序投放</label>
                      <input type="hidden" name="puttype" id="puttype">
                  </div>
                </div>
                <div class="form-group">
                  <label for="zone_name" class="col-sm-2 control-label">广告位名称</label>
                  <div class="col-sm-5">
                    <input type="text" name="zone_name" maxlength="50" class="form-control" id="zone_name">
                  </div>

                </div>


              <!--广告类型即广告样式id -->
                <div class="form-group">
                  <label for="zonetype" class="col-sm-2 control-label">广告类型</label>
                  <div class="col-sm-2">
                    <select id="zonetype"  class="form-control" name="zonetype">

                    </select>
                  </div>
                </div>

                <div class="form-group">
                  <label  class="col-sm-2 control-label">动画效果</label>

                  <div class="col-sm-4">
                    <label class="radio-inline">
                      <input  name="animation" type="radio" value="null" checked>无动画效果</label>
                    <label class="radio-inline">
                      <input name="animation" type="radio" value="animation">动画效果展示</label>
                  </div>
                </div>

                <div class="form-group" id="radioDiv">
                  <label  class="col-sm-2 control-label">漂浮位置</label>

                  <div class="col-sm-4">

                    <label class="radio-inline">
                      <input  name="position" type="radio" value="top" checked>固定顶部</label>
                    <label class="radio-inline">
                      <input name="position" type="radio" value="bottom">固定底部</label>
                  </div>
                </div>

				<div class="form-group">
                  <label for="countdown" class="col-sm-2 control-label">视频广告展示时长</label>
                  <div class="col-sm-5">
                    <input type="text" name="countdown" maxlength="50" class="form-control" id="countdown">
                  </div>
                </div>

                <div class="form-group">
                  <label for="size" class="col-sm-2 control-label">尺寸</label>
                  <div class="col-sm-2">
                    <select id="size" class="form-control">

                    </select>
                    <input type="hidden" name="width" class="form-control" id="width">
                    <input type="hidden" name="height" class="form-control" id="height">
                  </div>
                </div>

                <div class="form-group">
                  <label  class="col-sm-2 control-label">结算模式</label>

                    <div class="col-sm-4">
                      <label class="checkbox-inline">
                        <input type="checkbox" value="cpc" name="plantype" checked>cpc</label>
                      <label class="checkbox-inline">
                        <input type="checkbox" value="cpa" name="plantype" checked>cpa</label>
                      <label class="checkbox-inline">
                        <input type="checkbox" value="cpm" name="plantype" checked>cpm</label>
                      <label class="checkbox-inline">
                        <input type="checkbox" value="cpr" name="plantype" checked>cpr</label>
                      <label class="checkbox-inline">
                        <input type="checkbox" value="cps" name="plantype" checked>cps</label>
                    </div>
                </div>

              </div>


              <!-- /.box-body -->
              <div class="box-footer">
                <button type="button" onclick="goBack();" class="btn btn-default">取消</button>
                <button type="button" onclick="submitForm()" class="btn btn-primary">提交</button>
              </div>
              <!-- /.box-footer -->
            </form>
          </div>
        </div>
      </div>
    </section>
  </div>
  <!--底部-->
  <footer id="footerDiv" class="main-footer">
    <!--#include  file="footer.html"-->
  </footer>

  <!-- Control Sidebar -->
  <!-- /.control-sidebar -->
  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<script src="/bower_components/jquery/dist/jquery.min.js"></script>
<script src="/bower_components/morris.js/morris.min.js"></script>
<script src="/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="/bower_components/datatables.net/js/jquery.dataTables.min.js"></script>
<script src="/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
<script src="/bower_components/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<script src="/bower_components/fastclick/lib/fastclick.js"></script>
<script src="/dist/js/adminlte.min.js"></script>

<script src="/dist/js/layer/layer.js"></script>
<script src="/dist/js/bootstrapValidator.min.js"></script>

<script>
    var flag=true;
    var sizeMap={};
    var typeMap={};
    $(function () {
        //加载网站列表list

        initSiteList();
        initPuttype();
        initAdsZone();

        //表单验证
        $('#dataForm').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                siteid: {
                    validators: {
                        notEmpty: {
                            message: '请选择网站'
                        }

                    }
                },
                zonetype: {
                    validators: {
                        notEmpty: {
                            message: '请选择广告类型'
                        }

                    }
                }

            }
        });

        //根据类型样式id改变尺寸下拉框
        $("#zonetype").change(function () {
            let id = $(this).val();
            let sizes = sizeMap[id].split(",");
            let type_range = typeMap[id].split(",");
            let shtml = '';
            $.each(sizes, function (i, v) {
                shtml += '<option value="'+v+'">'+v+'</option>';
            });
            $("#size").html(shtml);
            $("input[name='plantype']").prop({checked:false}).attr("disabled",true);
            $.each(type_range, function (i, v) {
                $("input[value='"+v+"']").prop({checked:true}).removeAttr("disabled");
            });
            lockPosition();
        });

    });
    function submitForm() {
        if(!flag){
            return;
        }

        $('#dataForm').bootstrapValidator('validate');

        if($("#dataForm").data("bootstrapValidator").isValid()){
            flag=false;
            var val = $('input[type="radio"]:checked').val();
            $("#puttype").val(val);

            let size=$("#size").val();
            let width=size.split('*')[0];
            let height=size.split('*')[1];
            $("#width").val(width);
            $("#height").val(height);

            if($("#zone_name").val()==''){
                let zonename='创建于'+new Date().Format("yyyy-MM-dd HH:mm:ss");
                $("#zone_name").val(zonename);
            }
            let url="/lmgame/channel/saveAdsZone";
            $.ajax({
                type : 'POST',
                url : url,
                data :$("#dataForm").serialize(),
                dataType : "json",
                success: function(data) {
                    if (data.code != 0) {
                        layer.msg(data.msg);
                    } else {
                        layer.msg('新建成功', {
                            icon: 1,
                            time: 1000 //3秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            flag=true;
                            window.location.href = "/pages/channel/adsZoneList.html";
                        });
                    }
                },
                error: function (xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
                    flag=true;
                    if (xhr.status === 400) {
                        window.location.href = "/pages/channel/login.html";
                    }
                    if (xhr.status === 403) {
                        window.location.href = "/403.html";
                    }
                }
            });
        }
    }



    function initSiteList() {
        let url="/lmgame/channel/getSiteList";
        $.ajax({
            type : 'POST',
            url : url,
            data :{},
            dataType : "json",
            success: function(data) {
                let siteList=data.data;
                let str='<option  value="">请选择</option>';
                for(let i=0;i<siteList.length;i++){
                    let site = siteList[i];
                   str +='<option class="os_type'+site.os_type+'" value="'+site.id+'">'+site.site_name+'</option>';

                }
                $("#siteid").html(str);
            }
        });


    }


    /**
     * 加载样式尺寸列表
     */
    function initAdsZone() {
        //加载初始化数据
        let url="/lmgame/adsZone/getAdsZone";
        $.ajax({
            type : 'POST',
            url : url,
            data :{},
            dataType : "json",
            success: function(data) {
                let shtml = '';
                let styles = data.data.styles;
                let firstId='';
                $.each(styles, function (i, v) {
                    let value = v.id;
                    if(i==0){
                        shtml += '<option value="'+value+'" selected>'+v.name+'</option>';
                        firstId=value;
                    }else{
                        shtml += '<option value="'+value+'">'+v.name+'</option>';
                    }
                    sizeMap[value] = v.sizes;
                    typeMap[value] = v.type_range;
                });
                $("#zonetype").html(shtml);


                //初始化尺寸列表
                let id = firstId;
                let sizes = sizeMap[id].split(",");
                let shtml1 = '';
                $.each(sizes, function (i, v) {
                    shtml1 += '<option value="'+v+'">'+v+'</option>';
                });
                $("#size").html(shtml1);

                let type_range = typeMap[id].split(",");
                $("input[name='plantype']").prop({checked:false}).attr("disabled",true);
                $.each(type_range, function (i, v) {
                    $("input[value='"+v+"']").prop({checked:true}).removeAttr("disabled");
                });

                lockPosition();

            },
            error: function (xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
                flag=true;
                if (xhr.status === 400) {
                    window.location.href = "/pages/admin/login.html";
                }
                if (xhr.status === 403) {
                    window.location.href = "/403.html";
                }
            }
        });
    }

    
    function  initPuttype() {
        let str=$("#siteid").find("option:selected").attr("class");
        if(str=='os_type1') {
            $("#jstype").removeAttr("checked");
            $("#minitype").removeAttr("checked");
            $("#sdktype").attr("checked", "checked");
        }else if(str=='os_type2') {
            $("#jstype").removeAttr("checked");
            $("#sdktype").removeAttr("checked");
            $("#minitype").attr("checked", "checked");
        }else{
            $("#minitype").removeAttr("checked");
            $("#sdktype").removeAttr("checked");
            $("#jstype").attr("checked", "checked");
        }
    }

    function lockPosition() {
        let zonetype=$("#zonetype").val();
        if(zonetype!='2'){//漂浮 禁用position
            var input = $("#radioDiv").find("input:radio");
            input.attr("disabled","disabled");
        }else {
            var input = $("#radioDiv").find("input:radio");
            input.removeAttr("disabled");
        }
    }

    function  goBack() {
        window.history.back(-1);
    }

    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "H+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    };

</script>
</body>
</html>
